package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.user;
import com.example.springboot.service.loginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/loginController")
public class loginController {
    @Autowired
    private loginService loginservice;

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password1");


        List<user> user = loginservice.findByUsername(username);
        System.out.println(user);
        user user1 = user.get(0);

        if (user != null && user1.getPassword().equals(password)&&user1.getUsername().equals(username)) {
            // 登录成功，返回用户信息
            Map<String, Object> data = new HashMap<>();
            data.put("user_id", user1.getUser_id());
            data.put("username", user1.getUsername());
            data.put("role", user1.getRole());
            return Result.success(data);
        } else {
            // 登录失败，返回错误信息
            return Result.error("401", "用户名或密码错误");
        }
    }
}
